package com.tangkun.environment.fragment.news;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tangkun.environment.R;
import com.tangkun.environment.activity.SearchZiXunActivity;
import com.tangkun.environment.activity.WoTiJiaoDeActivity;
import com.tangkun.environment.activity.ZiXunAddActivity;
import com.tangkun.environment.adapter.ZiXunAdapter;
import com.tangkun.environment.entity.xinwen.QuestionReturn;
import com.tangkun.environment.fragment.BaseFragment;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxFragmentHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CustomListView;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 咨询Fragment
 */

public class ZiXunFragment extends BaseFragment {

    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;
    public int page = 1;// 当前页
    private int totalCount = 0;// 总数量
    private boolean OnHeadRefreshTAG = true;// 为true时清楚已有数据

    /**
     * 数据源
     */
    private List<QuestionReturn.DataBean> dataList = new ArrayList<QuestionReturn.DataBean>();
    /**
     * 适配器
     */
    private ZiXunAdapter adapter;

    /**
     * 咨询列表查询key参数
     */
    private String key = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_zixun, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        mContext = getActivity();
        initView();
        initData();
        return view;
    }

    private void initView() {
        adapter = new ZiXunAdapter(mContext, dataList);
        customListView.setAdapter(adapter);
    }

    private void initData() {
        page = 1;
        queryQuestion();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //创建了咨询，刷新咨询列表
    @Subscriber(tag = "ZiXunFragment.refresh")
    private void refresh(boolean flag) {
        initData();
    }

    //用户登录刷新咨询列表
    @Subscriber(tag = "MainActivity.refreshFragment")
    private void refreshFragment(boolean flag) {
        initData();
    }

    /**
     * 咨询检索
     */
    private void queryQuestion() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNumber", page + "");
        map.put("pageSize", pageSize + "");
        map.put("key", key + "");
        fromNetwork.queryQuestion(map)
                .compose(new RxFragmentHelper<QuestionReturn>().ioMain(mContext, ZiXunFragment.this, true))
                .subscribe(new MySubscribe<QuestionReturn>(mContext, true) {
                    @Override
                    protected void success(QuestionReturn resultBean) {
                        if (resultBean.data != null) {
                            //填充数据
                            dataList.clear();
                            dataList.addAll(resultBean.data);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    @OnClick({R.id.ll_main_chuangjian, R.id.ll_main_wode, R.id.ll_main_sousuo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_main_chuangjian://创建
                JumpHelper.jump(getActivity(), ZiXunAddActivity.class, false);
                break;
            case R.id.ll_main_wode://我的
                JumpHelper.jump(getActivity(), WoTiJiaoDeActivity.class, false);
                break;
            case R.id.ll_main_sousuo://搜索
                JumpHelper.jump(getActivity(), SearchZiXunActivity.class, false);
                break;
        }
    }
}